package ca.abubakrsagar.breadboardsim;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * An action listener for a text field to be used in the properties panel.
 * 
 * @author Sagar & Abubakr
 * @since June 14, 2012
 * 
 */
public class TextFieldController implements ActionListener {
	private BreadboardPanel breadboardPanel; // The breadboard panel.
	private PropertiesPanel propertiesPanel; // The properties panel.

	/**
	 * Associates the local panels to the specified panels.
	 * 
	 * @param propertiesPanel
	 *            The given properties panel.
	 * @param breadboardPanel
	 *            The given breadboard panel.
	 */
	public TextFieldController(PropertiesPanel propertiesPanel,
			BreadboardPanel breadboardPanel) {
		this.propertiesPanel = propertiesPanel;		
		this.breadboardPanel = breadboardPanel;
	}

	/**
	 * If the value in the text field is changed, update the resistors
	 * resistance value based on the given value.
	 */
	@Override
	public void actionPerformed(ActionEvent arg0) {
		breadboardPanel
				.getBreadboard()
				.getResistor()
				.get(propertiesPanel.getPosition())
				.setOhms(
						Integer.parseInt(propertiesPanel.getTextField()
								.getText()));
		propertiesPanel.display();
		propertiesPanel.repaint();
	}

}
